package com.deluxelx.soul.inn.web.oauth;

import cn.hutool.core.util.StrUtil;
import com.deluxelx.soul.inn.bean.user.oauth.AccountDTO;
import com.deluxelx.soul.inn.common.result.ResponseCode;
import com.deluxelx.soul.inn.common.result.SoulInnResult;
import com.deluxelx.soul.inn.service.oauth.OAuthService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 权限、登录注册控制器
 *
 * @author: zihao.liao
 * @date: 2024/1/18 23:49
 */
@RestController
@RequestMapping("/soul/inn/oauth")
public class OAuthController {

    @Resource
    private OAuthService oauthService;

    @PostMapping("/register")
    public SoulInnResult<String> register(@RequestBody AccountDTO accountDTO) {
        ResponseCode registerCode = oauthService.register(accountDTO);
        if (registerCode == ResponseCode.SUCCESS) {
            return SoulInnResult.success("注册成功");
        }
        return SoulInnResult.fail(registerCode);
    }

    @PostMapping("/login")
    public SoulInnResult<String> login(@RequestBody AccountDTO accountDTO) {
        String secretKey = oauthService.login(accountDTO);
        if (StrUtil.isNotEmpty(secretKey)) {
            return SoulInnResult.success(secretKey);
        }
        return SoulInnResult.fail("登录失败");
    }

}
